#ifndef IMAGEVIEW_H
#define IMAGEVIEW_H

#include <QWidget>

#include <QPixmap>
#include <QMenu>

#include "processing.h"
#include "ocrthread.h"
#include "nerthread.h"

enum ViewMode{
    LoadMode,
    IdleMode,
    DragMode,
    TextModeReady,
    TextMode,
};

class ImageView : public QWidget//, public std::enable_shared_from_this<ImageView>
{
    Q_OBJECT
public:
    explicit ImageView(QWidget *parent = nullptr);
    ~ImageView();
    void setImage(QString imgPath);
    void setPixmap(QPixmap pixmap);
    void startOCR();
    void startNER();
protected:

    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *event);
signals:


private slots:
    void nerTextFinished(int result);
    void ocrImageFinished(int result);
    void timerTimeOut();
    void copySelectText();
    void selectAll();
private:
//    void threadRun();
    void getText();
    void getLinkAndMail();
//    void getNerResult();
    void copyAll();
    void createRightMenu();
    void appendRightMenu();
    int getLineOfPos(QPoint pos);
    bool processPos2IfContained(QPoint pos2);
    void processPos2WhenAsc();
    void processPos2WhenDesc();
    void processPos1WhenAsc();
    void processPos1WhenDesc();
    bool comprePosition();
    void drawSingleCharOutlineAndLine(QPainter &painter);
    void drawScanner(QPainter &painter);
    void drawSelection(QPainter &painter);
    void calcAndShow();
    void triggerMenu(QAction *act);

    bool m_isCal = false;
    bool m_isPixmap = false;
    QString m_srcPath;
    QPixmap m_srcImg;
    QPixmap m_img;
    QPoint m_startPos;
    QPoint m_lastPos;
    QPoint m_endPos;
//    QPoint m_posStart;
//    QPoint m_posEnd;
    int m_newHeight;
    int m_newWidth;
    float m_scale ;
    QPoint m_point;
    bool m_wheelFlag = true;
    float m_lastScale;
    float m_x;
    float m_y;
    bool m_leftClick;
    int m_viewMode = LoadMode;//-1:nothing 0:drag 1:text

    int m_width;
    int m_height;

    QList<TextBox> m_cutBoxSet;
    QList<int> m_lineSet;
    QList<int> m_lineTopSet;
    float m_s1_pos;
    float m_s2_pos;
    int m_s1;
    int m_s2;

    QPoint m_pos1;
    QPoint m_pos2;
    int m_pos1_line;
    int m_pos2_line;

    int m_t1;
    int m_t2;
    int m_line;

    QList<NERThread *> m_nerThreads;
    NERThread *m_nerThread = nullptr;
    QList<OCRThread *> m_ocrThreads;
    OCRThread *m_ocrThread = nullptr;
    QTimer *m_timer;
    int m_timerIndex;
    int m_direction;
    QRect m_scanRect;

    QMenu *m_rightMenu = nullptr;
    QAction *m_copyAction;
    QAction *m_copyAllAction;
    QAction *m_selectAllAction;
    QActionGroup *m_linkAction;
    QActionGroup *m_mailAction;
    QActionGroup *m_nameAction;
    QActionGroup *m_addressAction;
    QActionGroup *m_positionAction;
    QActionGroup *m_phoneNumberAction;
//    QAction *m_translateAction;

    QString m_text;
    QMap<QString, QStringList> m_nerResult;
    std::map<std::string, std::vector<std::string>> m_grepResult;
    bool m_isSeparatorAdded = false;
};


#endif // IMAGEVIEW_H
